#include <algorithm>
#include <cstdio>
#include <cstring>

typedef long long ll;
const int N = 1e5 + 5;
int n, k, a[N], buk[N], L = 1, R;
ll val, f[25][N];
inline void add(int x) { val += buk[a[x]], buk[a[x]]++; }

inline void del(int x) { val -= --buk[a[x]]; }

inline ll calc(int l, int r) {
  while (R < r) add(++R);
  while (L > l) add(--L);
  while (R > r) del(R--);
  while (L < l) del(L++);
  return val;
}

void solve(int s, int t, int l, int r, int k) {
  if (s > t) return;
  int mid = (s + t) >> 1, p = l;
  for (int i = l; i <= std::min(mid, r); ++i) {
    if (f[k - 1][i - 1] + calc(i, mid) < f[k][mid]) {
      f[k][mid] = f[k - 1][i - 1] + calc(i, mid);
      p = i;
    }
  }
  solve(s, mid - 1, l, p, k);
  solve(mid + 1, t, p, r, k);
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("CF868F Yet Another Minimization Problem.in", "r", stdin);
  freopen("CF868F Yet Another Minimization Problem.out", "w", stdout);
#endif
#endif

  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  memset(f, 0x3f, sizeof(f));
  f[0][0] = 0;
  for (int i = 1; i <= k; ++i) { solve(1, n, 1, n, i); }
  printf("%lld", f[k][n]);
  return 0;
}